<?php

use \GatewayWorker\Lib\Gateway;  

require_once dirname(__DIR__) . '/public.class.model.php';
class Play_Model extends Public_Model
{

	/*
		抢庄
	*/
	public function grabBanker($arrData)
	{
		$timestamp = time();
		$result = array();
		
		$data 			= $arrData['data'];
		$operation 		= $arrData['operation'];
		$account_id 	= $arrData['account_id'];
		$client_id 		= $arrData['client_id'];

		$session 		= $arrData['session'];
		$Verification_Model = Verification_Model::getModelObject();
		if(false == $Verification_Model->checkRequestSession($account_id,$session))
		{
			return OPT_CONST::NO_RETURN;
		}
		
		if(!isset($data['room_id']) || $data['room_id']<=0)
		{
			$this->logMessage('error', "function(grabBanker):lack of room_id"." in file".__FILE__." on Line ".__LINE__);
			return $this->_missingPrameterArr($operation,"room_id");
		}
		if(!isset($data['is_grab']) || $data['is_grab'] === "")
		{
			$this->logMessage('error', "function(grabBanker):lack of is_grab"." in file".__FILE__." on Line ".__LINE__);
			return $this->_missingPrameterArr($operation,"is_grab");
		}

		$multiples = 1;	//抢庄倍数 默认1倍
		
		$room_id = $data['room_id'];
		$is_grab = $data['is_grab'];
		
		$game_num = $this->queryGameNumber($room_id);
		
		$room_rule = $this->queryRoomRule($room_id); 
		$banker_mode = $room_rule['banker_mode'];
		if($banker_mode == Game::BankerMode_FixedBanker && $game_num > 1)
		{
			$this->logMessage('error', "function(grabBanker): game_num($game_num) can not grab "." in file".__FILE__." on Line ".__LINE__);
			return array("result"=>OPT_CONST::FAILED,"operation"=>$operation,"data"=>$result,"result_message"=>"庄家已确定");
		}

		$account_status = $this->queryAccountStatus($room_id, $account_id);
		if(Redis_CONST::DATA_NONEXISTENT === $account_status || !in_array($account_status, [Game::AccountStatus_Choose]))
		{
			$this->logMessage('error', "function(grabBanker):account($account_id) status($account_status) error "." in file".__FILE__." on Line ".__LINE__);
			return array("result"=>OPT_CONST::FAILED,"operation"=>$operation,"data"=>$result,"result_message"=>"用户状态异常");
		}

		if(Game::PaymentType_AA == Config::Ticket_Mode && $this->queryTicketChecked($room_id, $account_id) != 1){
			//扣除房卡
			$spend_ticket_count = $this->queryTicketCount($room_id);
			$this->balanceTicket($room_id, $account_id, $spend_ticket_count);
		}

		if($is_grab)
		{
			//抢庄
			$status = Game::AccountStatus_Grab;
			$this->writeLog("[$room_id]($account_id) 抢庄");

			//添加用户抢庄倍数
			$this->updateGrabMultiples($room_id, $account_id,$multiples);
		} else {
			//不抢庄
			$status = Game::AccountStatus_Notgrab;

			$this->writeLog("[$room_id]($account_id) 不抢庄");
		}
		
		//设置用户状态
		$this->updateAccountStatus($room_id, $account_id, $status,$multiples);

		//是否开始叫分
		$is_bet = G_CONST::IS_TRUE;
		//获取所有游戏用户
		$table_user_array = $this->queryTableUser($room_id);
		foreach($table_user_array as $player_id)
		{
			//获取游戏用户状态
			$player_status = $this->queryAccountStatus($room_id, $player_id);
			if(!in_array($player_status, [Game::AccountStatus_Notgrab,Game::AccountStatus_Grab]))
			{
				$is_bet = G_CONST::IS_FALSE;
			}
		}

		if($is_bet == G_CONST::IS_TRUE)
		{
			$this->deleteRoomTimer($room_id);
			//选择庄家，开始洗牌回合
			$this->startShuffleRound($room_id);
		}

		return OPT_CONST::NO_RETURN;
	}

	/*
		下注
	*/
	public function throwChip($arrData)
	{
		$timestamp = time();
		$result = array();
		
		$data 			= $arrData['data'];
		$operation 		= $arrData['operation'];
		$account_id 	= $arrData['account_id'];
		$client_id 		= $arrData['client_id'];

		$session 		= $arrData['session'];
		$Verification_Model = Verification_Model::getModelObject();
		if(false == $Verification_Model->checkRequestSession($account_id,$session))
		{
			return OPT_CONST::NO_RETURN;
		}

		if(!isset($data['chip']) || !in_array($data['chip'], [10,20,30,50,80,100]) )
		{
			$this->logMessage('error', "function(chooseChip):lack of chip"." in file".__FILE__." on Line ".__LINE__);
			return $this->_missingPrameterArr($operation,"chip");
		}
		if(!isset($data['position']) || !in_array($data['position'], [0,1,2,3]) )
		{
			$this->logMessage('error', "function(chooseChip):lack of position"." in file".__FILE__." on Line ".__LINE__);
			return $this->_missingPrameterArr($operation,"position");
		}
		if(!isset($data['room_id']) || $data['room_id']<=0)
		{
			$this->logMessage('error', "function(chooseChip):lack of room_id"." in file".__FILE__." on Line ".__LINE__);
			return $this->_missingPrameterArr($operation,"room_id");
		}
		
		$chip = $data['chip'];
		$position = $data['position'];
		$room_id = $data['room_id'];

		//判断用户状态
		$account_status = $this->queryAccountStatus($room_id, $account_id);
		if(Redis_CONST::DATA_NONEXISTENT === $account_status || !in_array($account_status, [Game::AccountStatus_Bet]))
		{
			$this->logMessage('error', "function(throwChip):account($account_id) status($account_status) error "." in file".__FILE__." on Line ".__LINE__);
			return array("result"=>OPT_CONST::FAILED,"operation"=>$operation,"data"=>$result,"result_message"=>"用户状态异常");
		}

		$banker_id = $this->queryBanker($room_id);
		if($banker_id == $account_id)
		{
			$this->logMessage('error', "function(throwChip):banker can not throwChip "." in file".__FILE__." on Line ".__LINE__);
			return array("result"=>OPT_CONST::FAILED,"operation"=>$operation,"data"=>$result,"result_message"=>"用户状态异常");
		}

		if(Game::PaymentType_AA == Config::Ticket_Mode && $this->queryTicketChecked($room_id, $account_id) != 1){
			//扣除房卡
			$spend_ticket_count = $this->queryTicketCount($room_id);
			$this->balanceTicket($room_id, $account_id, $spend_ticket_count);
		}

		$Redis_Model = Redis_Model::getModelObject();
		$replyArr = array("[roomid]"=>$room_id);
		$TicketChecked_Key = strtr(Redis_Const::TicketChecked_Key, $replyArr);
		$Redis_Model->hmsetField($TicketChecked_Key, array($account_id=>1));

		//判断用户是否已叫分
		$pre_chip = $this->queryChip($room_id, $position, $account_id)['my_bet'];
		if($pre_chip > 0)
		{
			$this->writeLog("[$room_id]($account_id) has bet on position:".$position." with chip:".$chip);
			return array("result"=>OPT_CONST::FAILED,"operation"=>$operation,"data"=>$result,"result_message"=>"参数异常");
		}

		$this->updatePlayerChip($room_id, $account_id, $position ,$chip);

		$msg_arr = array(
			'result' => 0,
			'operation' => 'UpdateAccountChip',
			'result_message' => "下注",
			'data' => array(
				'account_id' => $account_id,
				'position'  => $position,
				'chip'  => $chip
				)
			);
		$this->pushMessageToGroup($room_id, $msg_arr);

		return OPT_CONST::NO_RETURN;
	}

	/*
		摊牌
	*/
	public function showCard($arrData)
	{
		$timestamp = time();
		$result = array();
		
		$data 			= $arrData['data'];
		$operation 		= $arrData['operation'];
		$account_id 	= $arrData['account_id'];
		$client_id 		= $arrData['client_id'];

		$session 		= $arrData['session'];
		$Verification_Model = Verification_Model::getModelObject();
		if(false == $Verification_Model->checkRequestSession($account_id,$session))
		{
			return OPT_CONST::NO_RETURN;
		}

		if(!isset($data['room_id']) || $data['room_id']<=0)
		{
			$this->logMessage('error', "function(showCard):lack of room_id"." in file".__FILE__." on Line ".__LINE__);
			return $this->_missingPrameterArr($operation,"room_id");
		}
		
		$room_id = $data['room_id'];

		//判断用户状态	
		$account_status = $this->queryAccountStatus($room_id, $account_id);
		if(Redis_CONST::DATA_NONEXISTENT === $account_status || !in_array($account_status, [Game::AccountStatus_Notshow]))
		{
			$this->writeLog($account_id." status($account_status) error "." in file".__FILE__." on Line ".__LINE__);
			return array("result"=>OPT_CONST::FAILED,"operation"=>$operation,"data"=>$result,"result_message"=>"用户状态异常");
		}

		//修改用户状态
		$this->updateAccountStatus($room_id, $account_id, Game::AccountStatus_Show);

		$card_info = $this->queryCardInfo($room_id);

		$Redis_Model = Redis_Model::getModelObject();
		$replyArr = array("[roomid]"=>$room_id);
		$RoomSequence_Key = strtr(Redis_Const::RoomSequence_Key, $replyArr);
		$seat_number = $Redis_Model->getZscore($RoomSequence_Key, $account_id);
		$i = $seat_number - 1;
		$offset = ($this->queryCircle($room_id) == Game::Circle_Show2) ? 8 : 0;

		$cards = array($card_info[2*$i + $offset], $card_info[2*$i+1 + $offset]);

		$card_value = $this->caculateCardValue($cards, $room_id);

		//通知用户修改状态
		$msg_arr = array(
			'result' => 0,
			'operation' => 'UpdateAccountShow',
			'result_message' => "摊牌",
			'data' => array(
				'seat_index' => $i
				)
			);
		$this->pushMessageToGroup($room_id, $msg_arr);

		//判断是否结算
		$is_win = G_CONST::IS_TRUE;
		//判断是否全部摊牌了
		$table_user_array = $this->queryTableUser($room_id);
		foreach($table_user_array as $player_id)
		{
			//获取游戏用户状态
			$player_status = $this->queryAccountStatus($room_id, $player_id);
			if($player_status != Game::AccountStatus_Show)
			{
				$is_win = G_CONST::IS_FALSE;
				break;
			}
		}

		if($is_win == G_CONST::IS_TRUE)
		{
			$this->deleteRoomTimer($room_id);
			//进入结算环节
			$this->startWinRound($room_id);
		}

		return OPT_CONST::NO_RETURN;
	}



	/*
		洗牌
	*/
	public function shuffleCard($arrData)
	{
		return OPT_CONST::NO_RETURN;
		$timestamp = time();
		$result = array();
		
		$data 			= $arrData['data'];
		$operation 		= $arrData['operation'];
		$account_id 	= $arrData['account_id'];
		$client_id 		= $arrData['client_id'];

		$session 		= $arrData['session'];
		$Verification_Model = Verification_Model::getModelObject();
		if(false == $Verification_Model->checkRequestSession($account_id,$session))
		{
			return OPT_CONST::NO_RETURN;
		}

		if(!isset($data['room_id']) || $data['room_id']<=0)
		{
			$this->logMessage('error', "function(shuffleCard):lack of room_id"." in file".__FILE__." on Line ".__LINE__);
			return $this->_missingPrameterArr($operation,"room_id");
		}
		
		$room_id = $data['room_id'];

		$circle = $this->queryCircle($room_id);
		if($circle != Game::Circle_Shuffle){
			$this->logMessage('error', "function(shuffleCard):account($account_id) circle($circle) error "." in file".__FILE__." on Line ".__LINE__);
			return array("result"=>OPT_CONST::FAILED,"operation"=>$operation,"data"=>$result,"result_message"=>"不在洗牌回合");
		} 

		if(Game::PaymentType_AA == Config::Ticket_Mode && $this->queryTicketChecked($room_id, $account_id) != 1){
			//扣除房卡
			$spend_ticket_count = $this->queryTicketCount($room_id);
			$this->balanceTicket($room_id, $account_id, $spend_ticket_count);
		}

		//通知用户修改状态
		$msg_arr = array(
			'result' => 0,
			'operation' => 'ShufflingCard',
			'result_message' => "洗牌",
			'data' => array(
				'account_id' => $account_id,
				)
			);
		$this->pushMessageToGroup($room_id, $msg_arr);
	
		$this->deleteRoomTimer($room_id);

		$this->startBetRound($room_id);

		return OPT_CONST::NO_RETURN;
	}

	/*
		切牌
	*/
	public function moveCard($arrData)
	{
		$timestamp = time();
		$result = array();
		
		$data 			= $arrData['data'];
		$operation 		= $arrData['operation'];
		$account_id 	= $arrData['account_id'];
		$client_id 		= $arrData['client_id'];

		$session 		= $arrData['session'];
		$Verification_Model = Verification_Model::getModelObject();
		if(false == $Verification_Model->checkRequestSession($account_id,$session))
		{
			return OPT_CONST::NO_RETURN;
		}

		if(!isset($data['room_id']) || $data['room_id']<=0)
		{
			$this->logMessage('error', "function(shuffleCard):lack of room_id"." in file".__FILE__." on Line ".__LINE__);
			return $this->_missingPrameterArr($operation,"room_id");
		}

		if(!isset($data['type']) || !in_array($data['type'], [1,2,3,4]))
		{
			$this->logMessage('error', "function(shuffleCard):lack of type"." in file".__FILE__." on Line ".__LINE__);
			return $this->_missingPrameterArr($operation,"type");
		}
		
		$room_id = $data['room_id'];
		$type = $data['type'];

		//通知用户修改状态
		$msg_arr = array(
			'result' => 0,
			'operation' => 'MovingCard',
			'result_message' => "切牌",
			'data' => array(
				'account_id' => $account_id,
				'type' => $type
				)
			);
		$this->pushMessageToGroup($room_id, $msg_arr);

		if($type==4){
			//$this->startDealRound($room_id);
		}
	
		return OPT_CONST::NO_RETURN;
	}


	/*
		发牌
	*/
	public function bankDealCard($arrData)
	{
		$timestamp = time();
		$result = array();
		
		$data 			= $arrData['data'];
		$operation 		= $arrData['operation'];
		$account_id 	= $arrData['account_id'];
		$client_id 		= $arrData['client_id'];

		$session 		= $arrData['session'];
		$Verification_Model = Verification_Model::getModelObject();
		if(false == $Verification_Model->checkRequestSession($account_id,$session))
		{
			return OPT_CONST::NO_RETURN;
		}

		if(!isset($data['room_id']) || $data['room_id']<=0)
		{
			$this->logMessage('error', "function(DealingCard):lack of room_id"." in file".__FILE__." on Line ".__LINE__);
			return $this->_missingPrameterArr($operation,"room_id");
		}

		if(!isset($data['type']) || !in_array($data['type'], [1,2,3,4]))
		{
			$this->logMessage('error', "function(DealingCard):lack of type"." in file".__FILE__." on Line ".__LINE__);
			return $this->_missingPrameterArr($operation,"type");
		}
		
		$room_id = $data['room_id'];
		$type = $data['type'];

		$circle = $this->queryCircle($room_id);
		if($circle != Game::Circle_Deal && $circle != Game::Circle_Deal2){
			$this->logMessage('error', "function(bankDealCard):account($account_id) circle($circle) error "." in file".__FILE__." on Line ".__LINE__);
			return array("result"=>OPT_CONST::FAILED,"operation"=>$operation,"data"=>$result,"result_message"=>"不在发牌回合");
		} 

		//通知用户修改状态
		$msg_arr = array(
			'result' => 0,
			'operation' => 'DealingCard',
			'result_message' => "发牌",
			'data' => array(
				'account_id' => $account_id,
				'type' => $type,
				'dice_a' => mt_rand(1, 6),
				'dice_b' => mt_rand(1, 6)
				)
			);
		$this->pushMessageToGroup($room_id, $msg_arr);

		$this->deleteRoomTimer($room_id);

		$this->startShowRound($room_id);
	
		return OPT_CONST::NO_RETURN;
	}


	/*
		发牌
	*/
	public function notySecondHalf($arrData)
	{
		$timestamp = time();
		$result = array();
		
		$data 			= $arrData['data'];
		$operation 		= $arrData['operation'];
		$account_id 	= $arrData['account_id'];
		$client_id 		= $arrData['client_id'];

		$session 		= $arrData['session'];
		$Verification_Model = Verification_Model::getModelObject();
		if(false == $Verification_Model->checkRequestSession($account_id,$session))
		{
			return OPT_CONST::NO_RETURN;
		}

		if(!isset($data['room_id']) || $data['room_id']<=0)
		{
			$this->logMessage('error', "function(DealingCard):lack of room_id"." in file".__FILE__." on Line ".__LINE__);
			return $this->_missingPrameterArr($operation,"room_id");
		}
		$room_id = $data['room_id'];

		//通知用户修改状态
		// $msg_arr = array(
		// 	'result' => 0,
		// 	'operation' => 'SecondHalf',
		// 	'result_message' => "下半场开始",
		// 	'data' => array(
		// 		)
		// 	);
		// $this->pushMessageToGroup($room_id, $msg_arr);

		$this->startBetRound($room_id);

	}


	/*
		下庄
	*/
	public function gameOver($arrData)
	{
		$timestamp = time();
		$result = array();
		
		$data 			= $arrData['data'];
		$operation 		= $arrData['operation'];
		$account_id 	= $arrData['account_id'];
		$client_id 		= $arrData['client_id'];

		$session 		= $arrData['session'];
		$Verification_Model = Verification_Model::getModelObject();
		if(false == $Verification_Model->checkRequestSession($account_id,$session))
		{
			return OPT_CONST::NO_RETURN;
		}

		if(!isset($data['room_id']) || $data['room_id']<=0)
		{
			$this->logMessage('error', "function(gameOver):lack of room_id"." in file".__FILE__." on Line ".__LINE__);
			return $this->_missingPrameterArr($operation,"room_id");
		}
		
		$room_id = $data['room_id'];

		//判断用户状态	
		$account_status = $this->queryAccountStatus($room_id, $account_id);
		if(Redis_CONST::DATA_NONEXISTENT === $account_status || $account_status != Game::AccountStatus_Notready)
		{
			$this->logMessage('error', "function(gameOver):account($account_id) status($account_status) error "." in file".__FILE__." on Line ".__LINE__);
			return array("result"=>OPT_CONST::FAILED,"operation"=>$operation,"data"=>$result,"result_message"=>"用户状态异常");
		}

		$this->logMessage('error', "function(gameOver):account($account_id) 解散房间 $room_id"." in file".__FILE__." on Line ".__LINE__);
		//解散房间
		$array['room_id'] = $room_id;
		$this->breakRoom($array);

		return OPT_CONST::NO_RETURN;
	}



	/*
		发送声音
	*/
	public function broadcastVoice($arrData)
	{
		$data 			= $arrData['data'];
		$operation 		= $arrData['operation'];
		$account_id 	= $arrData['account_id'];
		$client_id 		= $arrData['client_id'];

		$session 		= $arrData['session'];
		$Verification_Model = Verification_Model::getModelObject();
		if(false == $Verification_Model->checkRequestSession($account_id,$session))
		{
			return OPT_CONST::NO_RETURN;
		}
		
		if(!isset($data['room_id']) || $data['room_id']<=0)
		{
			$this->logMessage('error', "function(broadcastVoice):lack of room_id"." in file".__FILE__." on Line ".__LINE__);
			return $this->_missingPrameterArr($operation,"room_id");
		}
		if(!isset($data['voice_num']) || $data['voice_num'] === "")
		{
			$this->logMessage('error', "function(broadcastVoice):lack of voice_num"." in file".__FILE__." on Line ".__LINE__);
			return $this->_missingPrameterArr($operation,"voice_num");
		}
		
		$room_id = $data['room_id'];
		$voice_num = $data['voice_num'];
		
		//echo "用户".$account_id. " 说". $voice_num.PHP_EOL;
		$this->writeLog("[$room_id]($account_id) 说". $voice_num);

		$msg_arr = array("result"=>"0","operation"=>$operation,"data"=>array(
			'account_id'=>$account_id,
			'voice_num' =>$voice_num
			),"result_message"=>"发送声音");
		$this->pushMessageToGroup($room_id, $msg_arr, $client_id);
		return OPT_CONST::NO_RETURN;
	}

	/*
		获取在线数
	*/
	public function LxhServer($arrData)
	{
		$data                   = $arrData['data'];
		$operation              = $arrData['operation'];
		$client_count = Gateway::getAllClientCount();
		return array("result"=>0,"operation"=>$operation,"data"=>array('client_count'=>$client_count),"result_message"=>"当前在线连接总数");
	}

	
}
